package com.gomoney.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gomoney.auth.common.util.RedisUtil;
import com.gomoney.auth.mapper.SysUserMapper;
import com.gomoney.auth.common.util.RedisUtil;
import com.gomoney.auth.mapper.SysUserMapper;
import com.gomoney.auth.service.SysUserService;
import com.gomoney.oauth2.common.entity.sys.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;

public abstract class AbstractController {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysUserMapper sysUserMapper;

    protected SysUser getUser() {
        String username = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        SysUser sysUser = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUserName, username)
        );
        Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        authorities.forEach(a -> {
            sysUser.getPermsSet().add(a.getAuthority());
        });
        return sysUser;
    }
}
